Python Algorithmic Trading Cookbook¶

Chapter 4: Candlestick Patterns and Historical Data

This Jupyter Notebook is created using Python version 3.8.2


Master Recipe

The following code will help you set up the broker connection with Zerodha, which will be used by all the recipes in this chapter. Please make sure you have followed these steps before trying out any recipe.

In [1]:
from pyalgotrading.broker.broker_connection_zerodha import BrokerConnectionZerodha
In [2]:
# Get the api_key and api_secret from broker. These are unique to you and will be used by the broker to identify your demat account.
api_key = "5mrqgbb6ms4s73am"
api_secret = "dbrqbp3ketmvn2lav17ajwmfgy2i96w2"
broker_connection = BrokerConnectionZerodha(api_key, api_secret)
Please login to this link to generate your request token: https://kite.trade/connect/login?api_key=5mrqgbb6ms4s73am&v=3
In [3]:
# Get the request token from the above URL
request_token = "A6qlnYlXru9l87vK2r10EjWq7qk7uU2R"
broker_connection.set_access_token(request_token)

Recipe 1: Fetching Historical Data using the Broker API

In [4]:
# Fetch historical data for an instrument
instrument = broker_connection.get_instrument('NSE', 'TATASTEEL')
historical_data = broker_connection.get_historical_data(instrument=instrument, 
                                                        candle_interval='minute', 
                                                        start_date='2020-01-01', 
                                                        end_date='2020-01-01')
historical_data
Out[4]:
timestamp open high low close volume
0 2020-01-01 09:15:00+05:30 473.00 474.45 472.75 473.90 97244
1 2020-01-01 09:16:00+05:30 473.90 474.25 472.85 473.15 104766
2 2020-01-01 09:17:00+05:30 473.20 474.05 473.20 473.50 37920
3 2020-01-01 09:18:00+05:30 473.55 473.80 473.20 473.80 51424
4 2020-01-01 09:19:00+05:30 473.80 474.40 473.70 474.30 45331
... ... ... ... ... ... ...
370 2020-01-01 15:25:00+05:30 467.80 468.00 467.60 467.85 25907
371 2020-01-01 15:26:00+05:30 467.85 468.00 467.70 467.80 19267
372 2020-01-01 15:27:00+05:30 467.80 467.95 467.55 467.70 30234
373 2020-01-01 15:28:00+05:30 467.70 467.80 467.65 467.65 32464
374 2020-01-01 15:29:00+05:30 467.65 467.75 467.45 467.55 37885

375 rows × 6 columns

In [5]:
historical_data.columns
Out[5]:
Index(['timestamp', 'open', 'high', 'low', 'close', 'volume'], dtype='object')

Recipe 2: Historical Data as Japanese (OHLC) Candlesticks Pattern

In [6]:
from pyalgotrading.utils.func import plot_candlestick_chart, PlotType
In [7]:
# A 'Green' Japanese Candle
candle_green = historical_data.iloc[:1,:]            # Only 1st ROW of historical data
plot_candlestick_chart(candle_green, PlotType.JAPANESE, "A 'Green' Japanese Candle")
In [8]:
# A Red Japanese Candle
candle_red = historical_data.iloc[1:2,:]            # Only 2nd ROW of historical data
plot_candlestick_chart(candle_red, PlotType.JAPANESE, "A 'Red' Japanese Candle")
In [9]:
plot_candlestick_chart(historical_data, 
                        PlotType.JAPANESE, 
                        'Historical Data | Japanese Candlesticks Pattern | NSE:TATASTEEL | 1st Jan, 2020 | Candle Interval: 1 Minute')
In [10]:
instrument2 = broker_connection.get_instrument('NSE', 'INFY')
historical_data = broker_connection.get_historical_data(instrument2, 
                                                        'minute', 
                                                        '2020-01-01', 
                                                        '2020-01-01')
plot_candlestick_chart(historical_data, 
                        PlotType.JAPANESE, 
                        'Historical Data | Japanese Candlesticks Pattern | NSE:INFY | 1st Jan, 2020 | Candle Interval: 1 Minute')
In [11]:
instrument3 = broker_connection.get_instrument('NSE', 'ICICIBANK')
historical_data = broker_connection.get_historical_data(instrument3, 
                                                        'minute', 
                                                        '2020-01-01', 
                                                        '2020-01-01')
plot_candlestick_chart(historical_data, 
                        PlotType.JAPANESE, 
                        'Historical Data | Japanese Candlesticks Pattern | NSE:ICICIBANK | 1st Jan, 2020 | Candle Size: 1 Minute')

Recipe 3: Japanese Candlesticks Pattern with Variation in Candle Intervals

In [12]:
from pyalgotrading.utils.func import plot_candlestick_chart, PlotType
In [13]:
# Fetch an instrument
instrument = broker_connection.get_instrument('NSE', 'TATASTEEL')
In [14]:
historical_data_1minute = broker_connection.get_historical_data(instrument, 
                                                                'minute', 
                                                                '2020-01-01', 
                                                                '2020-01-01')
plot_candlestick_chart(historical_data_1minute, 
                        PlotType.JAPANESE, 
                        'Historical Data | Japanese Candlesticks Pattern | NSE:TATASTEEL | 1st Jan, 2020 | Candle Interval: 1 Minute')
In [15]:
historical_data_3minutes = broker_connection.get_historical_data(instrument, 
                                                                 '3minute', 
                                                                 '2020-01-01', 
                                                                 '2020-01-01')
plot_candlestick_chart(historical_data_3minutes, 
                        PlotType.JAPANESE, 
                        'Historical Data | Japanese Candlesticks Pattern | NSE:TATASTEEL | 1st Jan, 2020 | Candle Interval: 3 Minutes')
In [16]:
historical_data_5minutes = broker_connection.get_historical_data(instrument, 
                                                                 '5minute', 
                                                                 '2020-01-01', 
                                                                 '2020-01-01')
plot_candlestick_chart(historical_data_5minutes, 
                        PlotType.JAPANESE, 
                        'Historical Data | Japanese Candlesticks Pattern | NSE:TATASTEEL | 1st Jan, 2020 | Candle Interval: 5 Minutes')
In [17]:
historical_data_10minutes = broker_connection.get_historical_data(instrument, 
                                                                  '10minute', 
                                                                  '2020-01-01', 
                                                                  '2020-01-01')
plot_candlestick_chart(historical_data_10minutes, 
                        PlotType.JAPANESE, 
                        'Historical Data | Japanese Candlesticks Pattern | NSE:TATASTEEL | 1st Jan, 2020 | Candle Interval: 10 Minutes')
In [18]:
historical_data_15minutes = broker_connection.get_historical_data(instrument, 
                                                                  '15minute', 
                                                                  '2020-01-01', 
                                                                  '2020-01-01')
plot_candlestick_chart(historical_data_15minutes, 
                        PlotType.JAPANESE, 
                        'Historical Data | Japanese Candlesticks | NSE:TATASTEEL | 1st Jan, 2020 | Candle Interval: 15 Minutes')
In [19]:
historical_data_30minutes = broker_connection.get_historical_data(instrument, 
                                                                  '30minute', 
                                                                  '2020-01-01', 
                                                                  '2020-01-01')
plot_candlestick_chart(historical_data_30minutes, 
                        PlotType.JAPANESE, 
                        'Historical Data | Japanese Candlesticks Pattern | NSE:TATASTEEL | 1st Jan, 2020 | Candle Interval: 30 Minutes')
In [20]:
historical_data_1hour = broker_connection.get_historical_data(instrument, 
                                                              'hour', 
                                                              '2020-01-01', 
                                                              '2020-01-01')
plot_candlestick_chart(historical_data_1hour, 
                        PlotType.JAPANESE, 
                        'Historical Data | Japanese Candlesticks Pattern | NSE:TATASTEEL | 1st Jan, 2020 | Candle Interval: 1 Hour')
In [21]:
historical_data_day = broker_connection.get_historical_data(instrument, 
                                                            'day', 
                                                            '2020-01-01', 
                                                            '2020-01-01')
plot_candlestick_chart(historical_data_day, 
                        PlotType.JAPANESE, 
                        'Historical Data | Japanese Candlesticks Pattern | NSE:TATASTEEL | 1st Jan, 2020 | Candle Interval: Day')

Recipe 4: Historical Data as Linebreak Candlesticks Pattern

In [22]:
from pyalgotrading.utils.func import plot_candlestick_chart, PlotType
from pyalgotrading.utils.candlesticks.linebreak import Linebreak
In [23]:
instrument = broker_connection.get_instrument('NSE', 'TATASTEEL')
historical_data_1minute = broker_connection.get_historical_data(instrument, 
                                                                'minute', 
                                                                '2019-12-01', 
                                                                '2019-12-31')
historical_data_1minute_linebreak = Linebreak(historical_data_1minute)
historical_data_1minute_linebreak
Out[23]:
close open timestamp
0 424.00 424.95 2019-12-02 09:15:00+05:30
1 424.50 424.00 2019-12-02 09:16:00+05:30
2 425.75 424.80 2019-12-02 09:17:00+05:30
3 423.75 424.80 2019-12-02 09:19:00+05:30
4 421.70 423.75 2019-12-02 09:20:00+05:30
... ... ... ...
1058 474.90 474.55 2019-12-31 10:44:00+05:30
1059 471.60 474.55 2019-12-31 11:19:00+05:30
1060 471.50 471.60 2019-12-31 14:19:00+05:30
1061 471.35 471.50 2019-12-31 15:00:00+05:30
1062 471.00 471.35 2019-12-31 15:29:00+05:30

1063 rows × 3 columns

In [24]:
# A 'Green' Linebreak Candle
candle_green_linebreak = historical_data_1minute_linebreak.iloc[1:2,:]            # Only 2nd ROW of historical data
plot_candlestick_chart(candle_green_linebreak, 
                        PlotType.LINEBREAK, 
                        "A 'Green' Linebreak Candle")
In [25]:
# A Red Linebreak Candle
candle_red_linebreak = historical_data_1minute_linebreak.iloc[:1,:]            # Only 1st ROW of historical data
plot_candlestick_chart(candle_red_linebreak, 
                        PlotType.LINEBREAK, 
                        "A 'Red' Linebreak Candle")
In [26]:
plot_candlestick_chart(historical_data_1minute_linebreak, 
                        PlotType.LINEBREAK, 
                        'Historical Data | Linebreak Candlesticks Pattern | NSE:TATASTEEL | Dec, 2019 | Candle Interval: 1 Minute', True)
In [27]:
historical_data_3minutes = broker_connection.get_historical_data(instrument, 
                                                                 '3minute', 
                                                                 '2019-12-01', 
                                                                 '2019-12-31')
historical_data_3minutes_linebreak = Linebreak(historical_data_3minutes)
plot_candlestick_chart(historical_data_3minutes_linebreak, 
                        PlotType.LINEBREAK, 
                        'Historical Data | Linebreak Candlesticks Pattern | NSE:TATASTEEL | Dec, 2019 | Candle Interval: 3 Minutes', True)
In [28]:
historical_data_5minutes = broker_connection.get_historical_data(instrument, 
                                                                 '5minute', 
                                                                 '2019-12-01', 
                                                                 '2020-01-10')
historical_data_5minutes_linebreak = Linebreak(historical_data_5minutes)
plot_candlestick_chart(historical_data_5minutes_linebreak, 
                        PlotType.LINEBREAK, 
                        'Historical Data | Linebreak Candlesticks Pattern | NSE:TATASTEEL | Dec, 2019 | Candle Interval: 5 Minutes', True)
In [29]:
historical_data_10minutes = broker_connection.get_historical_data(instrument, 
                                                                  '10minute', 
                                                                  '2019-12-01', 
                                                                  '2020-01-10')
historical_data_10minutes_linebreak = Linebreak(historical_data_10minutes)
plot_candlestick_chart(historical_data_10minutes_linebreak, 
                        PlotType.LINEBREAK, 
                        'Historical Data | Linebreak Candlesticks Pattern | NSE:TATASTEEL | Dec, 2019 | Candle Interval: 10 Minutes', True)
In [30]:
historical_data_15minutes = broker_connection.get_historical_data(instrument, 
                                                                  '15minute', 
                                                                  '2019-12-01', 
                                                                  '2020-01-10')
historical_data_15minutes_linebreak = Linebreak(historical_data_15minutes)
plot_candlestick_chart(historical_data_15minutes_linebreak, 
                        PlotType.LINEBREAK, 
                        'Historical Data | Linebreak Candlesticks Pattern | NSE:TATASTEEL | Dec, 2019 | Candle Interval: 15 Minutes', True)
In [31]:
historical_data_30minutes = broker_connection.get_historical_data(instrument, 
                                                                  '30minute', 
                                                                  '2019-12-01', 
                                                                  '2020-01-10')
historical_data_30minutes_linebreak = Linebreak(historical_data_30minutes)
plot_candlestick_chart(historical_data_30minutes_linebreak, 
                        PlotType.LINEBREAK, 
                        'Historical Data | Linebreak Candlesticks Pattern | NSE:TATASTEEL | Dec, 2019 | Candle Interval: 30 Minutes', True)
In [32]:
historical_data_1hour = broker_connection.get_historical_data(instrument, 
                                                              'hour', 
                                                              '2019-12-01', 
                                                              '2020-01-10')
historical_data_1hour_linebreak = Linebreak(historical_data_1hour)
plot_candlestick_chart(historical_data_1hour_linebreak, 
                        PlotType.LINEBREAK, 
                        'Historical Data | Linebreak Candlesticks Pattern | NSE:TATASTEEL | Dec, 2019 | Candle Interval: 1 Hour', True)
In [33]:
historical_data_day = broker_connection.get_historical_data(instrument, 
                                                            'day', 
                                                            '2019-12-01', 
                                                            '2020-01-10')
historical_data_day_linebreak = Linebreak(historical_data_day)
plot_candlestick_chart(historical_data_day_linebreak, 
                        PlotType.LINEBREAK, 
                        'Historical Data | Linebreak Candlesticks Pattern | NSE:TATASTEEL | Dec, 2019 | Candle Interval: 1 Day', True)

Recipe 5: Historical Data as Renko Candlesticks Pattern

In [34]:
from pyalgotrading.utils.func import plot_candlestick_chart, PlotType
from pyalgotrading.utils.candlesticks.renko import Renko
In [35]:
instrument = broker_connection.get_instrument('NSE', 'TATASTEEL')
historical_data_1minute = broker_connection.get_historical_data(instrument, 
                                                                'minute', 
                                                                '2019-12-01', 
                                                                '2020-01-10')
historical_data_1minute_renko = Renko(historical_data_1minute)
historical_data_1minute_renko
Out[35]:
timestamp open close
0 2019-12-02 09:15:00+05:30 424.95 424.0
1 2019-12-02 09:20:00+05:30 424.00 422.0
2 2019-12-02 10:00:00+05:30 424.00 426.0
3 2019-12-02 10:12:00+05:30 424.00 422.0
4 2019-12-02 15:28:00+05:30 422.00 420.0
... ... ... ...
186 2020-01-10 10:09:00+05:30 488.00 490.0
187 2020-01-10 11:41:00+05:30 490.00 492.0
188 2020-01-10 13:31:00+05:30 490.00 488.0
189 2020-01-10 13:36:00+05:30 488.00 486.0
190 2020-01-10 14:09:00+05:30 486.00 484.0

191 rows × 3 columns

In [36]:
# A Green Renko Candle
candle_green_renko = historical_data_1minute_renko.iloc[2:3,:]            # Only 3rd ROW of historical data
plot_candlestick_chart(candle_green_renko, 
                        PlotType.RENKO, 
                        "A Green 'Renko' Candle")
In [37]:
# A Red Renko Candles
candle_red_renko = historical_data_1minute_renko.iloc[1:2,:]            # Only 2nd ROW of historical data
plot_candlestick_chart(candle_red_renko, 
                        PlotType.RENKO, 
                        "A 'Red' Renko Candle")
In [38]:
plot_candlestick_chart(historical_data_1minute_renko, 
                        PlotType.RENKO, 
                        'Historical Data | Renko Candlesticks Pattern | NSE:TATASTEEL | Dec, 2019 | Candle Interval: 1 Minute', True)
In [39]:
historical_data_3minutes = broker_connection.get_historical_data(instrument, 
                                                                 '3minute', 
                                                                 '2019-12-01', 
                                                                 '2019-12-31')
historical_data_3minutes_renko = Renko(historical_data_3minutes)
plot_candlestick_chart(historical_data_3minutes_renko, 
                        PlotType.RENKO, 
                        'Historical Data | Renko Candlesticks Pattern | NSE:TATASTEEL | Dec, 2019 | Candle Interval: 3 Minutes', True)
In [40]:
historical_data_5minutes = broker_connection.get_historical_data(instrument, 
                                                                 '5minute', 
                                                                 '2019-12-01', 
                                                                 '2019-12-31')
historical_data_5minutes_renko = Renko(historical_data_5minutes)
plot_candlestick_chart(historical_data_5minutes_renko, 
                        PlotType.RENKO, 
                        'Historical Data | Renko Candlesticks Pattern | NSE:TATASTEEL | Dec, 2019 | Candle Interval: 5 Minutes', True)
In [41]:
historical_data_10minutes = broker_connection.get_historical_data(instrument, 
                                                                  '10minute', 
                                                                  '2019-12-01', 
                                                                  '2019-12-31')
historical_data_10minutes_renko = Renko(historical_data_10minutes)
plot_candlestick_chart(historical_data_10minutes_renko, 
                        PlotType.RENKO, 
                        'Historical Data | Renko Candlesticks Pattern | NSE:TATASTEEL | Dec, 2019 | Candle Interval: 10 Minutes', True)
In [42]:
historical_data_15minutes = broker_connection.get_historical_data(instrument, 
                                                                  '15minute', 
                                                                  '2019-12-01', 
                                                                  '2019-12-31')
historical_data_15minutes_renko = Renko(historical_data_15minutes)
plot_candlestick_chart(historical_data_15minutes_renko, 
                        PlotType.RENKO, 
                        'Historical Data | Renko Candlesticks Pattern | NSE:TATASTEEL | Dec, 2019 | Candle Interval: 15 Minutes', True)
In [43]:
historical_data_30minutes = broker_connection.get_historical_data(instrument, 
                                                                  '30minute', 
                                                                  '2019-12-01', 
                                                                  '2019-12-31')
historical_data_30minutes_renko = Renko(historical_data_30minutes)
plot_candlestick_chart(historical_data_30minutes_renko, 
                        PlotType.RENKO, 
                        'Historical Data | Renko Candlesticks Pattern | NSE:TATASTEEL | Dec, 2019 | Candle Interval: 30 Minutes', True)
In [44]:
historical_data_1hour = broker_connection.get_historical_data(instrument, 
                                                              'hour', 
                                                              '2019-12-01', 
                                                              '2019-12-31')
historical_data_1hour_renko = Renko(historical_data_1hour)
plot_candlestick_chart(historical_data_1hour_renko, 
                        PlotType.RENKO, 
                        'Historical Data | Renko Candlesticks Pattern | NSE:TATASTEEL | Dec, 2019 | Candle Interval: 1 Hour', True)
In [45]:
historical_data_day = broker_connection.get_historical_data(instrument, 
                                                            'day', 
                                                            '2019-12-01', 
                                                            '2019-12-31')
historical_data_day_renko = Renko(historical_data_day)
plot_candlestick_chart(historical_data_day_renko, 
                        PlotType.RENKO, 
                        'Historical Data | Renko Candlesticks Pattern | NSE:TATASTEEL | Dec, 2019 | Candle Interval: Day', True)

Recipe 6: Historical Data as Heikin-Ashi Candlesticks Pattern

In [46]:
from pyalgotrading.utils.func import plot_candlestick_chart, PlotType
from pyalgotrading.utils.candlesticks.heikinashi import HeikinAshi
In [47]:
instrument = broker_connection.get_instrument('NSE', 'TATASTEEL')
historical_data_1minute = broker_connection.get_historical_data(instrument, 
                                                                'minute', 
                                                                '2019-12-01', 
                                                                '2019-12-31')
historical_data_1minute_heikinashi = HeikinAshi(historical_data_1minute)
historical_data_1minute_heikinashi
Out[47]:
timestamp open high low close
0 2019-12-02 09:15:00+05:30 424.475000 424.950000 422.550000 424.1125
1 2019-12-02 09:16:00+05:30 424.293750 424.900000 423.400000 424.2000
2 2019-12-02 09:17:00+05:30 424.246875 426.000000 424.246875 425.2375
3 2019-12-02 09:18:00+05:30 424.742188 425.700000 424.500000 425.1250
4 2019-12-02 09:19:00+05:30 424.933594 425.200000 423.100000 424.2125
... ... ... ... ... ...
7870 2019-12-31 15:25:00+05:30 472.098314 472.200000 471.800000 471.9875
7871 2019-12-31 15:26:00+05:30 472.042907 472.042907 471.600000 471.7250
7872 2019-12-31 15:27:00+05:30 471.883954 471.950000 471.600000 471.7750
7873 2019-12-31 15:28:00+05:30 471.829477 471.950000 471.450000 471.7750
7874 2019-12-31 15:29:00+05:30 471.802238 471.802238 470.650000 471.3125

7875 rows × 5 columns

In [48]:
# A 'Green' HeikinAshi Candle
candle_green_heikinashi = historical_data_1minute_heikinashi.iloc[2:3,:]            # Only 3rd ROW of historical data
plot_candlestick_chart(candle_green_heikinashi, 
                        PlotType.HEIKINASHI, 
                        "A 'Green' HeikinAshi Candle")
In [49]:
# A 'Red' HeikinAshi Candle
candle_red_heikinashi = historical_data_1minute_heikinashi.iloc[4:5,:]            # Only 1st ROW of historical data
plot_candlestick_chart(candle_red_heikinashi, 
                        PlotType.HEIKINASHI, 
                        "A 'Red' HeikinAshi Candle")
In [50]:
plot_candlestick_chart(historical_data_1minute_heikinashi, 
                        PlotType.HEIKINASHI, 
                        'Historical Data | Heikin-Ashi Candlesticks Pattern | NSE:TATASTEEL | Dec, 2019 | Candle Interval: 1 minute', True)
In [51]:
historical_data_3minutes = broker_connection.get_historical_data(instrument, 
                                                                 '3minute', 
                                                                 '2019-12-01', 
                                                                 '2019-12-31')
historical_data_3minutes_heikinashi = HeikinAshi(historical_data_3minutes)
plot_candlestick_chart(historical_data_3minutes_heikinashi, 
                        PlotType.HEIKINASHI, 
                        'Historical Data | Heikin-Ashi Candlesticks Pattern | NSE:TATASTEEL | Dec, 2019 | Candle Interval: 3 minutes', True)
In [52]:
historical_data_5minutes = broker_connection.get_historical_data(instrument, 
                                                                 '5minute', 
                                                                 '2019-12-01', 
                                                                 '2019-12-31')
historical_data_5minutes_heikinashi = HeikinAshi(historical_data_5minutes)
plot_candlestick_chart(historical_data_5minutes_heikinashi, 
                        PlotType.HEIKINASHI, 
                        'Historical Data | Heikin-Ashi Candlesticks Pattern | NSE:TATASTEEL | Dec, 2019 | Candle Interval: 5 minutes', True)
In [53]:
historical_data_10minutes = broker_connection.get_historical_data(instrument, 
                                                                  '10minute', 
                                                                  '2019-12-01', 
                                                                  '2019-12-31')
historical_data_10minutes_heikinashi = HeikinAshi(historical_data_10minutes)
plot_candlestick_chart(historical_data_10minutes_heikinashi, 
                        PlotType.HEIKINASHI, 
                        'Historical Data | Heikin-Ashi Candlesticks Pattern | NSE:TATASTEEL | Dec, 2019 | Candle Interval: 10 minutes', True)
In [54]:
historical_data_15minutes = broker_connection.get_historical_data(instrument, 
                                                                  '15minute', 
                                                                  '2019-12-01', 
                                                                  '2019-12-31')
historical_data_15minutes_heikinashi = HeikinAshi(historical_data_15minutes)
plot_candlestick_chart(historical_data_15minutes_heikinashi, 
                        PlotType.HEIKINASHI, 
                        'Historical Data | Heikin-Ashi Candlesticks Pattern | NSE:TATASTEEL | Dec, 2019 | Candle Interval: 15 minutes', True)
In [55]:
historical_data_30minutes = broker_connection.get_historical_data(instrument, 
                                                                  '30minute', 
                                                                  '2019-12-01', 
                                                                  '2019-12-31')
historical_data_30minutes_heikinashi = HeikinAshi(historical_data_30minutes)
plot_candlestick_chart(historical_data_30minutes_heikinashi, 
                        PlotType.HEIKINASHI, 
                        'Historical Data | Heikin-Ashi Candlesticks Pattern | NSE:TATASTEEL | Dec, 2019 | Candle Interval: 30 minutes', True)
In [56]:
historical_data_1hour = broker_connection.get_historical_data(instrument, 
                                                              'hour', 
                                                              '2019-12-01', 
                                                              '2019-12-31')
historical_data_1hour_heikinashi = HeikinAshi(historical_data_1hour)
plot_candlestick_chart(historical_data_1hour_heikinashi, 
                        PlotType.HEIKINASHI, 
                        'Historical Data | Heikin-Ashi Candlesticks Pattern | NSE:TATASTEEL | Dec, 2019 | Candle Interval: 1 Hour', True)
In [57]:
historical_data_day = broker_connection.get_historical_data(instrument, 
                                                            'day', 
                                                            '2019-12-01', 
                                                            '2019-12-31')
historical_data_day_heikinashi = HeikinAshi(historical_data_day)
plot_candlestick_chart(historical_data_day_heikinashi, 
                        PlotType.HEIKINASHI, 
                        'Historical Data | Heikin-Ashi Candlesticks Pattern | NSE:TATASTEEL | Dec, 2019 | Candle Interval: Day', True)

Recipe 7: Fetch Historical Data using Quandl

In [58]:
from pyalgotrading.utils.func import plot_candlestick_chart, PlotType
import quandl
In [59]:
facebook = quandl.get('WIKI/FB', 
                      start_date='2015-1-1', 
                      end_date='2015-3-31')
plot_candlestick_chart(facebook, 
                        PlotType.QUANDL_JAPANESE, 
                        'Historical Data | Japanese Candlesticks Pattern | FACEBOOK | Jan-March 2015 | Candle Interval: Day', True)
In [60]:
amazon = quandl.get('WIKI/AMZN', 
                    start_date='2015-1-1', 
                    end_date='2015-3-31')
plot_candlestick_chart(amazon, 
                        PlotType.QUANDL_JAPANESE, 
                        'Historical Data | Japanese Candlesticks Pattern | AMAZON | Jan-March 2015 | Candle Interval: Day', True)
In [61]:
apple = quandl.get('WIKI/AAPL', 
                   start_date='2015-1-1', 
                   end_date='2015-3-31')
plot_candlestick_chart(apple, 
                        PlotType.QUANDL_JAPANESE, 
                        'Historical Data | Japanese Candlesticks Pattern | APPLE | Jan-March 2015 | Candle Interval: Day', True)
In [62]:
microsoft = quandl.get('WIKI/MSFT', 
                       start_date='2015-1-1', 
                       end_date='2015-3-31')
plot_candlestick_chart(microsoft, 
                        PlotType.QUANDL_JAPANESE, 
                        'Historical Data | Japanese Candlesticks Pattern | MICROSOFT | Jan-March 2015 | Candle Interval: Day', True)
In [63]:
google = quandl.get('WIKI/GOOGL', 
                    start_date='2015-1-1', 
                    end_date='2015-3-31')
plot_candlestick_chart(google, 
                        PlotType.QUANDL_JAPANESE, 
                        'Historical Data | Japanese Candlesticks Pattern | GOOGLE | Jan-March 2015 | Candle Interval: Day', True)